<template>
  <div>
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">
        <a-form ref="FormRef" layout="vertical" :model="FormState" :rules="FormRule">
          <a-form-item label="原密码" name="password">
            <a-input-password v-model:value="FormState.password" placeholder="请输入原密码" />
          </a-form-item>
          <a-form-item label="新密码" name="new_password">
            <a-input-password v-model:value="FormState.new_password" placeholder="请输入新密码" />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" :loading="ConfirmLoading" @click="OnSubmit">
              立即修改
            </a-button>
            <a-button style="margin-left: 8px" @click="OnReset">重置</a-button>
          </a-form-item>
        </a-form>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts">
import { IconFont } from '@/components/iconfont'
import { defineComponent, reactive, toRefs, ref, toRaw } from 'vue'
import { Row, Col } from 'ant-design-vue'
import { changePwd } from '@/api/personal'
import { requestFailed, requestSuccess } from '@/utils/feedback'

export default defineComponent({
  name: 'AccountPassword',
  components: {
    IconFont,
    [Row.name]: Row,
    [Col.name]: Col
  },
  setup() {
    const FormRef = ref()
    const state = reactive({
      ConfirmLoading: false
    })
    const FormState = reactive({
      new_password: '',
      password: ''
    })
    const FormRule = {
      password: [{ required: true, message: '请填写原密码', trigger: ['blur', 'change'] }],
      new_password: [{ required: true, message: '请填写新密码', trigger: ['blur', 'change'] }]
    }

    const OnSubmit = () => {
      FormRef.value
        .validate()
        .then(() => {
          changePwd(toRaw(FormState))
            .then((res) => {
              if (res.code === 200) {
                requestSuccess(res)
              } else {
                requestFailed(res)
              }
            })
            .finally(() => {
              state.ConfirmLoading = false
            })
        })
        .catch(() => {
          return false
        })
    }
    const OnReset = () => {
      FormRef.value.resetFields()
    }
    return {
      ...toRefs(state),
      FormRef,
      FormState,
      FormRule,
      OnSubmit,
      OnReset
    }
  }
})
</script>

<style lang="scss" scoped>
.account-settings-info-main {
  width: 100%;
  display: flex;
  height: 100%;
  overflow: auto;

  &.mobile {
    display: block;

    .account-settings-info-left {
      border-right: unset;
      border-bottom: 1px solid #e8e8e8;
      width: 100%;
      height: 50px;
      overflow-x: auto;
      overflow-y: scroll;
    }
    .account-settings-info-right {
      padding: 20px 40px;
    }
  }

  .account-settings-info-left {
    border-right: 1px solid #e8e8e8;
    width: 224px;
  }

  .account-settings-info-right {
    flex: 1 1;
    padding: 8px 40px;

    .account-settings-info-title {
      color: rgba(0, 0, 0, 0.85);
      font-size: 20px;
      font-weight: 500;
      line-height: 28px;
      margin-bottom: 12px;
    }
    .account-settings-info-view {
      padding-top: 12px;
    }
  }
}
</style>
